package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.inventoryproduct.pojo.dto.InventoryProductDeleteBatchDTO;
import com.xbongbong.pro.inventoryproduct.pojo.dto.InventoryProductListDTO;
import com.xbongbong.pro.inventoryproduct.pojo.vo.InventoryProductDeleteBatchVO;
import com.xbongbong.pro.inventoryproduct.pojo.vo.InventoryProductListVO;
import com.xbongbong.saas.domain.entity.InventoryProductEntity;
import com.xbongbong.saas.model.InventoryProductModel;
import com.xbongbong.saas.service.InventoryProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2019/2/28 16:14
 * @since v1.0
 */
@Service("inventoryProductService")
public class InventoryProductServiceImpl implements InventoryProductService {

    private static final Logger LOG = LoggerFactory.getLogger(InventoryProductServiceImpl.class);

    @Resource
    private InventoryProductModel inventoryProductModel;

    @Override
    public InventoryProductListVO findInventoryProductList(InventoryProductListDTO inventoryProductListDTO) throws XbbException {
        InventoryProductListVO inventoryProductListVO = new InventoryProductListVO();
        try {
            Map<String, Object> param = BeanUtil.convertBean2Map(inventoryProductListDTO, true);
            List<InventoryProductEntity> inventoryProductList = inventoryProductModel.findEntitys(param);
            if (Objects.isNull(inventoryProductList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            inventoryProductListVO.setInventoryProductList(inventoryProductList);
        } catch (Exception e) {
            LOG.error("transferProductServiceImpl.findTransferProductList 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return inventoryProductListVO;
    }

    @Override
    public InventoryProductDeleteBatchVO deleteBatch(InventoryProductDeleteBatchDTO inventoryProductDeleteBatchDTO) throws XbbException {

        InventoryProductDeleteBatchVO inventoryProductDeleteBatchVO = new InventoryProductDeleteBatchVO();
        try {
            List<Long> idList = inventoryProductDeleteBatchDTO.getIdList();
            String corpid = inventoryProductDeleteBatchDTO.getCorpid();
            if (!idList.isEmpty()) {
                inventoryProductModel.deleteBatch(idList, corpid, DelEnum.DELETE.getDel());
            }
        } catch (Exception e) {
            LOG.error("InventoryProductServiceImpl.deleteBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return inventoryProductDeleteBatchVO;
    }
}
